Public Class Form1    
    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        TextBox1.Text = e.Node.Text & " selecionado."

        If e.Node.GetNodeCount(True) > 0 Then
            Label2.Text = "Quantidade de nós: " & e.Node.GetNodeCount(True)
            Label3.Text = "Primeiro nó: " & e.Node.FirstNode.Text
            Label4.Text = "Último nó: " & e.Node.LastNode.Text
        Else
            Label2.Text = "Quantidade de nós: 0"
            Label3.Text = "Primeiro nó: N/A"
            Label4.Text = "Último nó: N/A"
        End If

        If Not e.Node.PrevNode Is Nothing Then
            Label6.Text = "Nó anterior: " & e.Node.PrevNode.Text
        Else
            Label6.Text = "Nó anterior: N/A"
        End If

        If Not e.Node.NextNode Is Nothing Then
            Label7.Text = "Próximo nó: " & e.Node.NextNode.Text
        Else
            Label7.Text = "Próximo nó: N/A"
        End If

        Label5.Text = "Caminho: " & e.Node.FullPath
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TreeView1.CheckBoxes = Not TreeView1.CheckBoxes
    End Sub

    Private Sub TreeView1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
        If e.Node.Checked Then
            TextBox1.Text = e.Node.Text & " marcado."
        Else
            TextBox1.Text = e.Node.Text & " desmarcado."
        End If        
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label2.Text = "Quantidade de nós: " & TreeView1.Nodes.Count        
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            TreeView1.ExpandAll()
            CheckBox1.Text = "Fechar tudo"
        Else
            TreeView1.CollapseAll()
            CheckBox1.Text = "Expandir tudo"
        End If
    End Sub
End Class
